Language transitions are defined in lang elements in the type’s xml file. They are used to indicate when an different type definition should be loaded. For example in php files the <?= ?> sequence is used to indicate that a section should be treated as php and handled server side.
php.xml
<lang type="php" colour="red" start="<?=" end="?>">
<startType type="html" id="*" />
<startType type="js" id="*" />
<startType type="css" id="*" />
<endType type="php" id="0" />
The lang element has a set of attributes on it which define how the language transition works. The startType elements define the type name and state id combination in which the language transition can start and endType elements define the type name and state id combination in which the language transition can end. For example a commented out style element shouldn’t cause a transition.
Attribute | Description | Default Value |
type | The name of the type to transition to | “” |
colour | The colour style to wrap the start and end character sequences in. The special "~inLang" option indicates that the formatting of the start and end sequences are handled in the specified type | “” |
start | The character sequence used to trigger language transition. The special “~base” option indicates that this language transition should be applied before starting to format the file and that it can’t end | “” |
end | The character sequence used to trigger the transition back | “” |
Code
Lang.h
Type: Header file
Language: C++
Lang.h Lang.cpp
The Lang class parses lang elements from the type’s xml file and stores the information to be queried by other parts of the application. It also contains methods to check for the start and end of a language transition and for printing the start and end sequence.
The Lang(std::string startType) constructor builds a simple language transition using the specified string as the type and “~base” as the start sequence. This is used to build an initial language object for types that don’t define a different type as the base.
The Lang(xmlNodePtr xmlNode) constructor first sets the lang attributes to their default values and then uses the passed in XML object to decode the attributes from the type file along with start and end TypeIdPairs.
The IsStart(std::string line, int pos, TypeIdPair testType) method compares the start sequence for the language transition against the characters in the current line at the specified position. If the start sequence matches it then loops through the list of allowed start type name and state ids to see if any of them match the passed in pair. If the start sequence matches and a type name and state id pair is found the method returns true, otherwise it returns false.
The IsEnd(std::string line, int pos, TypeIdPair testType) method compares the end sequence for the language transition against the characters in the current line at the specified position. If the end sequence matches it then loops through the list of allowed end type name and state ids to see if any of them match the passed in pair. If the end sequence matches and a type name and state id pair is found the method returns true, otherwise it returns false.
The PrintStart(std::stringstream& lineStream) method prints the start sequence to the lineStream. If the colour is not set to inLang then it prints the start sequence along with an opening and closing span and returns the start sequence length. Otherwise it prints nothing and lets the new language handle the formatting.
The PrintEnd(std::stringstream& lineStream) method prints the end sequence to the lineStream. If the colour is not set to inLang then it prints the end sequence along with an opening and closing span and returns the end sequence length. Otherwise it prints nothing and lets the previous language handle the formatting.